﻿using System.Windows.Forms;
using Ast.Framework.Environment.AutofacUtil;
using Autofac;
using Castle.Core.Internal;

namespace Ast.Framework.Forms
{
    internal class FormModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            // assemblies
            var assemblies = AutofacAssemblyStore.GetAssemblies();

            // factory
            builder.RegisterType<FormFactory>().As<IFormFactory>().InstancePerMatchingLifetimeScope(AutofacScope.Site);

            // forms
            builder.RegisterAssemblyTypes(assemblies)
                .Where(type => typeof (Form).IsAssignableFrom(type) && type.HasAttribute<FormMetadataAttribute>())
                .Keyed<Form>(type => type)
                .Named<Form>(type => type.GetAttribute<FormMetadataAttribute>().Type)
                .As<Form>()
                .WithMetadataFrom<IFormMetadata>()
                .InstancePerMatchingLifetimeScope(AutofacScope.Form);

            // form event handlers
            builder.RegisterSource(new FormEventHandlerRegistrationSource());
        }
    }
}